local japi   = require 'jass.japi'
local bignum = require "jass.bignum"
local md5    = japi.md5 
local rsa = {}

-- RSA公钥
rsa.e = "00010001"
rsa.n = "a87441ebe810751e23ec1341315b0f3a87cb58f8e96b1ccaf03f5a6b7258c4dad563f2f533e04759a7e954c9a7e8ecd8f161a2830f5dc4e9dc66253aff85ac744940d368587307b64ba00c7b02d4df6583057126d1960591078be9a1c212bf54571f1e9a30525010ca5e93329010545966c569d6b58b38502d55d4096bf8e26d"

-- RSA私钥
local suc, key = pcall(require, '(ppk)')
rsa.d = suc and key

function rsa:init()
    if self.e then self.e_bn = bignum.new(bignum.bin(self.e)) end
    if self.n then self.n_bn = bignum.new(bignum.bin(self.n)) end
    if self.d then self.d_bn = bignum.new(bignum.bin(self.d)) end
end

function rsa:encrypt(c)
    local c_bn = bignum.new(c)
    local m_bn = c_bn:powmod(self.e_bn, self.n_bn)
    return tostring(m_bn)
end

function rsa:decrypt(m)
    local m_bn = bignum.new(m)
    local c_bn = m_bn:powmod(self.d_bn, self.n_bn)
    return tostring(c_bn)
end

rsa:init()

local sha1 = bignum.sha1

--生成签名
--	文本
function rsa:get_sign(content)
	return self:decrypt(sha1(content))
end

--验证签名
--	文本
--	签名
function rsa:check_sign(content, sign)
	return sha1(content) == self:encrypt(sign)
end

return rsa
